package com.example.finalapplication;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class SecondTask implements Runnable {


    private static final String TAG = "SecondTask";
    private Handler handler;

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ArrayList<HashMap<String, String>> retList = new ArrayList<HashMap<String, String>>();
        try {


            // Document doc = Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
         /*   Document doc = Jsoup.connect("http://www.xinsinong.com/price/list-2212.html").get();

            String title = doc.title();
            Log.i(TAG, "run:title=" + title);

          Elements trs = doc.getElementsByTag("tr");
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                Log.i(TAG, "run:td.size=" + tds.size());
                if (tds.size() > 0) {
                    String td1 = tds.get(0).text();
                    String td2 = tds.get(2).text();
                    Log.i(TAG, "run:" + td2 + "==>" + td1);*/

           /* Element link = doc.select("a").first();
             String linkText = link.text();*/
           /* Elements urls = doc.select("li").select("a");
            for (int k = 0; k < urls.size(); ++k) {

                Element urll = urls.get(k); // 获取一个a

                Elements url1 = urll.select("a"); // 获取该行的所a节点

                for (int y = 0; y < url1.size(); ++y) {// 选择某一个a节点
                    Element url11 = url1.get(y);
                    //获取文本信息
                    String text2 = url11.text();
                    //输出到控制台
                    Log.i(TAG, "run:" + text2);

                    //构造map放入数据
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("cname", text2);
                    // map.put("cval",td1);

                    retList.add(map);


                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }*/
            Document doc = Jsoup.connect("http://www.xinsinong.com/price/list-2212.html").get();
            String title = doc.title();
            Log.i(TAG, "run:title=" + title);
            Elements uls = doc.getElementsByTag("ul");
            for (int i=1;i<=uls.size();i++) {

                Element ul = uls.get(i);
                Elements lis = ul.getElementsByTag("li");
                for (Element li : lis) {
                    Elements as = li.getElementsByTag("a");
                    Log.i(TAG, "run:as.size=" + as.size());
                    if (as.size() > 0) {
                        String fruitstitle = as.get(0).text();

                        Log.i(TAG, "run:" + fruitstitle);
                         HashMap<String, String> map = new HashMap<String, String>();
                     map.put("city", fruitstitle);

                    }
                }
            }
           /* Elements link = doc.select("ul.singer-con-index").select("a");
 //遍历该表格内的所有的<a> <a/>
             for (int i = 0; i < link.size(); ++i) {
     // 获取一个a
                Element url1 = link.get(i);
     // 获取该行的所a节点
               Elements names = url1.select("a");
     // 选择某一个a节点
             Log.i(TAG, "url:" + link.get(i).attr("href"));

             String fruitstitle = link.get(i).attr("href");
              HashMap<String, String> map = new HashMap<String, String>();
              Log.i(TAG, "run:" + fruitstitle);
                     map.put("city", fruitstitle);

             retList.add(map);
    // System.out.println(fruitstitle);

 }*/

        } catch (IOException e) {
            e.printStackTrace();
        }

        Message msg = handler.obtainMessage(6, retList);
        //msg.obj = "Hello";

        handler.sendMessage(msg);
        Log.i(TAG, "run:sendMessage ok");
    }
}